import {
  getDeviceAmount,
  getRepariMount,
  getAssayAmount,
  getCheckAmount,
  getMaintainAmount,
  approvalPending,
  dailyTask,
} from 'service/actions/workBench'

export default {
  namespace: 'workBench',

  state: {
    deviceAmount: {},
    repariMount: {},
    assayAmount: {},
    checkAmount: {},
    maintainAmount: {},
    approvalPending: {},
    dailyTask: {},
  },

  effects: {
    *init({ _ }, { put }){
      yield put({ type: 'deviceAmountData'})
      yield put({ type: 'repariMount'})
      yield put({ type: 'checkAmount'})
      yield put({ type: 'assayAmount'})
      yield put({ type: 'maintainAmount'})
      yield put({ type: 'getApprovalPending'})
    },

    // 获取设备数量
    *deviceAmountData ({ payload }, { put, call }){
      const data = yield call(getDeviceAmount, payload)
      yield put({
        type: 'updateState',
        payload: { deviceAmount: data.data}
      })
    },

    // 获取维修任务数量
    *repariMount ({ _ }, { put, call }){
      const data = yield call(getRepariMount)
      yield put({
        type: 'updateState',
        payload: { repariMount: data.data}
      })
    },

    // 获取检定计划数量
    *assayAmount ({ payload }, { put, call }){
      const data = yield call(getAssayAmount)
      yield put({
        type: 'updateState',
        payload: { assayAmount: data.data}
      })
    },

    // 获取点检计划数量
    *checkAmount ({ payload }, { put, call }){
      const data = yield call(getCheckAmount, payload)
      yield put({
        type: 'updateState',
        payload: {checkAmount: data.data}
      })
    },

    // 获取保养计划数量
    *maintainAmount ({ payload }, { put, call }){
      const data = yield call(getMaintainAmount, payload)
      yield put({
        type: 'updateState',
        payload: { maintainAmount: data.data}
      })
    },

    // 待审批统计
    *getApprovalPending ({ payload }, { put, call }){
      const data = yield call(approvalPending, {})
      yield put({
        type: 'updateState',
        payload: { approvalPending: data.data}
      })
    },

    // 今日任务
    *getDailyTask ({ payload }, { put, call }){
      const data = yield call(dailyTask, payload)
      yield put({
        type: 'updateState',
        payload: { dailyTask: data.data}
      })
    },
  },

  reducers: {
    // 更新List
    updateState(state, { payload }) {
      return { ...state, ...payload}
    }, 
    // 更新List
    clean(state, { payload }) {
      return { 
        ...state,
        dailyTask: {}
      }
    }, 
  }
}
